import 'package:fish_redux/fish_redux.dart';
import 'package:kunlun_gas/app_files.dart';
import 'package:date_format/date_format.dart';
import 'action.dart';
import 'state.dart';

Effect<MachineOrderViewState> buildEffect() {
  return combineEffects(<Object, Effect<MachineOrderViewState>>{
    // MachineOrderViewAction.action: _onAction,
    Lifecycle.initState: _initState,
    MachineOrderViewAction.onLoad: _onLoad,
    MachineOrderViewAction.onTapTime: _onTapTime,
  });
}

void _initState(Action action, Context<MachineOrderViewState> ctx) async {
  ResultData res = await CommonNetWork.machineUserInfo(ctx.state.userID);
  if (!res.success) {
    await CommonUtils.showAlertView(ctx.context, res.desc);
    Navigator.pop(ctx.context);
  }
  Map list = res.data['list'];
  List items = list['Data'];
  if (items.isEmpty) {
    await CommonUtils.showAlertView(ctx.context, '未找到用户信息');
    Navigator.pop(ctx.context);
    return;
  }
  Map info = items.first;
  String lastRecordTime =
      CommonUtils.checkNullToString(info['UAmountLastChaobiaoDate']);
  DateTime dateTime = DateTime.tryParse(lastRecordTime);

  ctx.state.model = MachineOrderModel(
    lateFee: CommonUtils.checkNullToString(info['UAmountLateFee']),
    balance: CommonUtils.checkNullToString(info['UAmountBalance']),
    lastPointNumber:
        CommonUtils.checkNullToString(info['UAmountChaobiaoNumber']),
    arrearsFee: CommonUtils.checkNullToString(info['UAmountArrearsFee']),
    gasMeterNo: CommonUtils.checkNullToString(info['GasMeterNo']),
    lastRecordTime:
        dateTime == null ? '-' : formatDate(dateTime, [yyyy, '-', mm, '-', dd]),
    meterFactory: '机械表',
  );

  // DateTime now = DateTime.now();
  // ctx.state.startTime =
  //     ctx.state.endTime = formatDate(now, [yyyy, '-', mm, '-', dd]);
  _sendRequest(ctx, 1);
  ctx.dispatch(MachineOrderViewActionCreator.onAction());
}

_sendRequest(Context<MachineOrderViewState> ctx, int page) async {
  ResultData res = await CommonNetWork.machineUserBillList(
      userID: ctx.state.userID,
      startTime: ctx.state.startTime,
      endTime: ctx.state.endTime,
      page: page);
  if (res.success) {
    List data = res.data;
    List<MachineOrderItemModel> items = data
        .map<MachineOrderItemModel>((e) => MachineOrderItemModel.fromJson(e))
        .toList();
    ctx.dispatch(MachineOrderViewActionCreator.onGetData(
        {'page': page, 'items': items}));
  } else {
    CommonUtils.showErrorInfo(ctx.context, res.desc);
    List<MachineOrderItemModel> items = [];
    ctx.dispatch(MachineOrderViewActionCreator.onGetData(
        {'page': page, 'items': items}));
  }
}

void _onLoad(Action action, Context<MachineOrderViewState> ctx) async {
  await _sendRequest(ctx, action.payload);
}

void _onTapTime(Action action, Context<MachineOrderViewState> ctx) async {
  DateTime now = DateTime.now();
  DateTime startTime = await CommonUtils.showMyDatePicker(
    ctx.context,
    desc: '请选择业务开始时间',
    initialDateTime: now,
    minimumDate: now.add(Duration(days: -60)),
    maximumDate: now,
  );
  if (startTime != null) {
    DateTime endTime = await CommonUtils.showMyDatePicker(
      ctx.context,
      desc: '请选择业务结束时间',
      initialDateTime: now,
      minimumDate: startTime,
      maximumDate: now,
    );
    print('-----------$startTime  $endTime');
    if (endTime != null) {
      ctx.state.startTime = formatDate(startTime, [yyyy, '-', mm, '-', dd]);
      ctx.state.endTime = formatDate(endTime, [yyyy, '-', mm, '-', dd]);
      ctx.state.items = null;
      ctx.dispatch(MachineOrderViewActionCreator.onAction());
      await _sendRequest(ctx, 1);
    }
  } else {
    ctx.state.startTime = ctx.state.endTime = null;
    ctx.dispatch(MachineOrderViewActionCreator.onAction());
    await _sendRequest(ctx, 1);
  }
}
